<div class="layui-card-header">
    <h2 class="header-title">修改密码</h2>
</div>
<div class="layui-card-body" style="padding: 30px;">
    <form class="layui-form" id="form-psw">
        <div class="layui-form-item">
            <input type="password" name="oldPasswd" required lay-verify="required|psw" class="layui-input"
                   placeholder="请输入原密码" lay-verType="tips">
        </div>
        <div class="layui-form-item">
            <input type="password" name="newPasswd" required lay-verify="required|psw" class="layui-input"
                   placeholder="请输入新密码" lay-verType="tips">
        </div>
        <div class="layui-form-item">
            <input type="password" name="repPasswd" required lay-verify="required|repsw" class="layui-input"
                   placeholder="再次输入新密码" lay-verType="tips">
        </div>
        <div class="layui-form-item">
            <button class="layui-btn layui-btn-fluid" lay-submit lay-filter="submit-psw">确认修改</button>
        </div>
    </form>
</div>

<script>
    layui.use(['layer', 'jquery', 'form', 'sadmin'], function () {
        var $ = layui.$ //重点处
        var layer = layui.layer;
        var form = layui.form;
        var sadmin = layui.sadmin;

        //监听提交
        form.on('submit(submit-psw)', function (data) {
            layer.load(2);
            $.post('/home/setPasswd', data.field, function (result) {
                layer.closeAll('loading');
                if (result.success) {
                    sadmin.closePopupRight();
                    layer.msg(result.msg);
                }
                else {
                    layer.closeAll('loading');
                    layer.msg(result.msg);
                }
            });
            return false;
        });

        //添加表单验证方法
        form.verify({
            psw: [/^[\S]{5,12}$/, '密码必须5到12位，且不能出现空格'],
            repsw: function (t) {
                if (t !== $('#form-psw input[name=newPasswd]').val()) {
                    return '两次密码输入不一致';
                }
            }
        });
    });
</script>